package com.sskj.bibi.ui.fragment;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.bibi.R;
import com.sskj.bibi.R2;
import com.sskj.bibi.presenter.PankouFragmentPresenter;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.BibiCoinType;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.WSFiveBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

//@Route(path = RConfig.BIBI_FRAGMENT_PANKOU)
public class PankouFragment extends BaseFragment<PankouFragmentPresenter> {
    @BindView(R2.id.tvPrice)
    TextView tvPrice;
    @BindView(R2.id.tvRMBPrice)
    TextView tvRMBPrice;
    @BindView(R2.id.tvRate)
    TextView tvRate;
    @BindView(R2.id.tvBuyTitle)
    TextView tvBuyTitle;
    @BindView(R2.id.tvSellTitle)
    TextView tvSellTitle;
    @BindView(R2.id.tvNumTitle)
    TextView tvNumTitle;
    @BindView(R2.id.buyRecyclerView)
    RecyclerView buyRecyclerView;
    @BindView(R2.id.sellRecyclerView)
    RecyclerView sellRecyclerView;
    @Autowired
    String code;
    @Autowired
    String qu;
    private SlimAdapter buySlimAdapter;
    private SlimAdapter sellSlimAdapter;


    @Override
    protected int getLayoutId() {
        return R.layout.bibi_fragment_pankou;
    }

    @Override
    public PankouFragmentPresenter getPresenter() {
        return new PankouFragmentPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        RxBus.getDefault().register(this);
        buyRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        sellRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        buySlimAdapter = SlimAdapter.create().register(R.layout.bibi_recy_item_pankou_buy, new SlimInjector<WSFiveBean.FiveBean>() {
            @Override
            public void onInject(WSFiveBean.FiveBean data, IViewInjector injector, List payloads) {

                injector
                        .text(R.id.tvPrice, data.getPrice())
                        .text(R.id.tvNum, data.getTotalSize())
                        .textColor(R.id.tvPrice, ContextCompat.getColor(App.INSTANCE, R.color.bibiGreenText))
//                        .with(R.id.bibiProgressbar, view -> {
//                            ProgressBar progressBar = (ProgressBar) view;
//                            progressBar.setProgressDrawable(ContextCompat.getDrawable(App.INSTANCE, R.drawable.bibi_progress_green));
//                            int progress = new Random(SystemClock.currentThreadTimeMillis()).nextInt(100);
//                            progressBar.setProgress(progress);
//                        })
                ;
            }
        });
        buySlimAdapter.attachTo(buyRecyclerView).updateData(new ArrayList());
        sellSlimAdapter = SlimAdapter.create().register(R.layout.bibi_recy_item_pankou_sell, new SlimInjector<WSFiveBean.FiveBean>() {
            @Override
            public void onInject(WSFiveBean.FiveBean data, IViewInjector injector, List payloads) {
                injector
                        .text(R.id.tvPrice, data.getPrice())
                        .text(R.id.tvNum, data.getTotalSize())
                        .textColor(R.id.tvPrice, ContextCompat.getColor(App.INSTANCE, R.color.bibiRedText))
//                        .with(R.id.bibiProgressbar, view -> {
//                            ProgressBar progressBar = (ProgressBar) view;
//                            progressBar.setProgressDrawable(ContextCompat.getDrawable(App.INSTANCE, R.drawable.bibi_progress_red));
//                            int progress = new Random(SystemClock.currentThreadTimeMillis()).nextInt(100);
//                            progressBar.setProgress(progress);
//                        })
                ;
            }
        });
        sellSlimAdapter.attachTo(sellRecyclerView).updateData(new ArrayList());
    }

    @Override
    public void initData() {
        updateCoin(new BibiCoinType(code));
    }

    /**
     * 设置买五卖五UI --
     */
    public void resetFiveUI() {
        WSFiveBean wsFiveBean = new WSFiveBean();
        wsFiveBean.setCode(code);
        ArrayList<WSFiveBean.FiveBean> asks = new ArrayList<>();
        ArrayList<WSFiveBean.FiveBean> bids = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            asks.add(new WSFiveBean.FiveBean(null, null));
            bids.add(new WSFiveBean.FiveBean(null, null));
        }
        wsFiveBean.setAsks(asks);
        wsFiveBean.setBids(bids);
        updateFive(wsFiveBean);
    }

    /**
     * 刷新页面
     */
    @Subscribe(code = RxBusCode.BIBI_FRESH, threadMode = ThreadMode.MAIN)
    public void refresh() {
        mPresenter.getPankou(code);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateFive(WSFiveBean wsFiveBean) {
        if (wsFiveBean != null && wsFiveBean.getCode() != null) {
            if (wsFiveBean.getCode().equals(code)) {
//                if (wsFiveBean != null) {
//                    if (wsFiveBean.getBids() == null) {
//                        ArrayList<WSFiveBean.FiveBean> bids = new ArrayList<>();
//                        for (int i = 0; i < 5; i++) {
//                            bids.add(new WSFiveBean.FiveBean("0.0000", "0.0000"));
//                        }
//                        wsFiveBean.setBids(bids);
//                    } else if (wsFiveBean.getBids().size() < 5) {
//                        for (int i = wsFiveBean.getBids().size(); i < 5; i++) {
//                            wsFiveBean.getBids().add(new WSFiveBean.FiveBean("0.0000", "0.0000"));
//                        }
//                    }
//                    if (wsFiveBean.getAsks() == null) {
//                        ArrayList<WSFiveBean.FiveBean> asks = new ArrayList<>();
//                        for (int i = 0; i < 5; i++) {
//                            asks.add(new WSFiveBean.FiveBean("0.0000", "0.0000"));
//                        }
//                        wsFiveBean.setAsks(asks);
//                    } else if (wsFiveBean.getAsks().size() < 5) {
//                        for (int i = wsFiveBean.getAsks().size(); i < 5; i++) {
//                            wsFiveBean.getAsks().add(new WSFiveBean.FiveBean("0.0000", "0.0000"));
//                        }
//                    }
//                } else {
//                    wsFiveBean = new WSFiveBean();
//                    wsFiveBean.setCode(code);
//                    ArrayList<WSFiveBean.FiveBean> asks = new ArrayList<>();
//                    ArrayList<WSFiveBean.FiveBean> bids = new ArrayList<>();
//                    for (int i = 0; i < 5; i++) {
//                        asks.add(new WSFiveBean.FiveBean("0.0000", "0.0000"));
//                        bids.add(new WSFiveBean.FiveBean("0.0000", "0.0000"));
//                    }
//                    wsFiveBean.setAsks(asks);
//                    wsFiveBean.setBids(bids);
//                }
                buySlimAdapter.updateData(wsFiveBean.getBids());
                sellSlimAdapter.updateData(wsFiveBean.getAsks());
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateCoin(BibiCoinType bibiCoinType) {
        code = bibiCoinType.getCode();
        tvBuyTitle.setText(String.format(App.INSTANCE.getString(R.string.bibipankouFragment1), code.replace("_", "/").split("/")[1]));
        tvSellTitle.setText(String.format(App.INSTANCE.getString(R.string.bibipankouFragment2), code.replace("_", "/").split("/")[1]));
        tvNumTitle.setText(String.format(App.INSTANCE.getString(R.string.bibipankouFragment3), code.replace("_", "/").split("/")[0]));
        resetFiveUI();
        mPresenter.getProduct(qu,code);
        mPresenter.getPankou(code);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateWsCoin(CoinBean productBean) {
        if (code.equals(productBean.getCode())) {
            updateTitle(productBean);
        }
    }

    public void updateTitle(CoinBean stockCoinBean) {
        if (tvPrice == null)
            return;
        try {
            tvPrice.setText(stockCoinBean.getPrice());
//            if (code.toUpperCase().contains("BDD")) {
//                if (TextUtils.isEmpty(rate_bdd)) {
//                    tvRMBPrice.setText(String.format("≈￥%s", NumberUtil.keep2(new BigDecimal(stockCoinBean.getPrice()).multiply(new BigDecimal(rate_bdd)).toPlainString())));
//                } else {
//                    tvRMBPrice.setText(String.format("≈￥%s", NumberUtil.keep2(stockCoinBean.getPrice())));
//                }
//            } else {
//            }
            tvRMBPrice.setText(String.format("≈%sUSDT", stockCoinBean.getCnyPrice()));
            tvRate.setText(stockCoinBean.getChangeRate());
            tvRate.setTextColor(ContextCompat.getColor(App.INSTANCE, Double.valueOf(stockCoinBean.getChange()) >= 0 ? R.color.bibiGreenText : R.color.bibiRedText));
            tvPrice.setTextColor(ContextCompat.getColor(App.INSTANCE, Double.valueOf(stockCoinBean.getChange()) >= 0 ? R.color.bibiGreenText : R.color.bibiRedText));
        } catch (Exception e) {

        }

    }


}
